package com.payment.web.controller.app;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.payment.common.base.AjaxResult;
import com.payment.system.domain.IssBank;
import com.payment.system.service.IIssBankService;
import com.payment.system.service.IMerchantIncreaseLimitService;

/**
 * @author fuzhili
 * @Description: 信用卡管理
 * @date 2019/1/23 16:50
 */
@Controller
@RequestMapping("/app/creditCard")
public class CreditCardController {
    private static final Logger log = LoggerFactory.getLogger(CreditCardController.class);
    @Autowired
    private IMerchantIncreaseLimitService merchantIncreaseLimitService;
    @Autowired
    private IIssBankService issBankService;

    /**
     * 获取信用卡列表
     *
     * @param merchantId 用户ID
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public AjaxResult list(String merchantId) {
        return merchantIncreaseLimitService.selectMerchantIncreaseLimitByMerchantId(merchantId);
    }

    /**
     * 获取指定信用卡信息
     *
     * @param cardId
     * @return
     */
    @RequestMapping(value = "/selectByCardId")
    @ResponseBody
    public AjaxResult selectByCardId(String cardId) {
        return merchantIncreaseLimitService.selectCardVoById(cardId);
    }

    /**
     * 绑定信用卡 获取银行信息
     *
     * @param cardNumber 信用卡号
     * @return
     */
    @PostMapping(value = "/getCreditCardType")
    @ResponseBody
    public AjaxResult saveCreditCard(String cardNumber) {
        try {
            return merchantIncreaseLimitService.getCreditCardType(cardNumber);
        } catch (Exception e) {
            log.error("获取信用卡信息失败，请联系客服查看原因！{}", e);
            return AjaxResult.error("获取信用卡信息失败，请联系客服查看原因！");
        }
    }

	/**
	 * app 商户添加信用卡获取验证码
	 *
	 * @param merchantId
	 *            商户ID
	 * @param companyId
	 *            公司ID
	 * @param bankAccountName
	 *            姓名
	 * @param bankAccount
	 *            信用卡号
	 * @param idCardNumber
	 *            身份证号
	 * @param bankPhone
	 *            预留手机号
	 * @param bankName
	 *            银行名称
	 * @param cvn
	 *            安全码cvn
	 * @param expdate
	 *            有效期
	 * @param trxSourceIp
	 *            调用段ip地址
	 * @return
	 * @throws Exception
	 */
    @PostMapping(value = "/bindCardMsg")
    @ResponseBody
    public AjaxResult bindCardMsg(String merchantId,String companyId, String bankAccountName,
                                  String bankAccount, String idCardNumber, String bankPhone, String bankName, String cvn, String expdate, String trxSourceIp) {
        try {
            return merchantIncreaseLimitService.bindCardMsg(merchantId,companyId, bankAccountName,
                    bankAccount, idCardNumber, bankPhone, bankName, cvn, expdate, trxSourceIp);
        } catch (Exception e) {
            log.error("信用卡发送验证码失败，请联系客服查看原因！{}", e);
            return AjaxResult.error("信用卡发送验证码失败，请联系客服查看原因！");
        }
    }

	/**
	 * app 商户新增信用卡
	 *
	 * @param merchantId
	 *            用户ID
	 * @param acqMerchantNo
	 *            上游商户id
	 * @param companyId
	 *            公司ID
	 * @param bankAccountName
	 *            姓名
	 * @param bankAccount
	 *            信用卡号
	 * @param idCardNumber
	 *            身份证号
	 * @param bankPhone
	 *            预留手机号
	 * @param bankName
	 *            银行名称
	 * @param cvn
	 *            安全码CVN
	 * @param expDate
	 *            有效期
	 * @param trxSourceIp
	 *            调用段ip地址
	 * @param orderNo
	 *            签约订单号，需保证唯一
	 * @param repaymentDay
	 *            还款日
	 * @param billDay
	 *            账单日
	 * @param limitMoney
	 *            额度
	 * @param smsCode
	 *            短信验证码
	 * @param smsType  0需要   1不需要验证码
	 *            是否需要短信验证码            
	 */
	@PostMapping(value = "/saveCreditCard")
	@ResponseBody
	public AjaxResult saveCreditCard(String merchantId, String acqMerchantNo, String companyId, String bankAccountName,
			String bankAccount, String idCardNumber, String bankPhone, String bankName, String cvn, String expDate,
			String trxSourceIp, String orderNo, String repaymentDay, String billDay, String limitMoney,
			String smsCode,@RequestParam(defaultValue="0")Integer smsType) {
		try {
			return merchantIncreaseLimitService.insertMerchantIncreaseLimit(merchantId, acqMerchantNo, companyId,
					bankAccountName, bankAccount, idCardNumber, bankPhone, bankName, cvn, expDate, trxSourceIp,
					repaymentDay, billDay, limitMoney, orderNo, smsCode,smsType);
		} catch (Exception e) {
			log.error("添加信用卡失败，请联系客服查看原因！{}", e);
			return AjaxResult.error("添加信用卡失败，请联系客服查看原因！");
		}
	}

    /**
     * 查看信用卡是否开通通道
     *
     * @param cardId
     * @return
     */
    @PostMapping(value = "/getcardBind")
    @ResponseBody
    public AjaxResult getcardBind(String cardId) {
        return merchantIncreaseLimitService.getcardBind(cardId);
    }

    /**
     * APP 用户解除信用卡绑定
     *
     * @param merchantId 用户id
     * @param cardId     信用卡号
     * @param paymentPwd 支付密码
     * @return
     */
    @PostMapping(value = "/removeCreditCard")
    @ResponseBody
    @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
    public AjaxResult removeCreditCard(String merchantId, String cardId, String paymentPwd) {
        try {
            return merchantIncreaseLimitService.removeCreditCardByCardIdAndMerchantId(merchantId, cardId, paymentPwd);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            log.error("解绑信用卡失败，请联系客服查看原因！{}", e);
            return AjaxResult.error("解绑信用卡失败，请联系客服查看原因！");
        }
    }

    /**
     * 修改卡信息 ：账单日，还款日 额度
     *
     * @param merchantId   用户id
     * @param cardId       信用卡id
     * @param billDay      账单日
     * @param repaymentDay 还款日
     * @param limitMoney  额度
     * @return
     */
    @PostMapping(value = "/updateCreditCard")
    @ResponseBody
    public AjaxResult updateCreditCard(String merchantId, String cardId, String billDay, String repaymentDay, String limitMoney) {
        return merchantIncreaseLimitService.updateCreditCard(merchantId, cardId, billDay, repaymentDay, limitMoney);
    }

	/**
	 * 获取所有银行列表
	 *
	 * @return
	 */
	@RequestMapping(value = "/getIssBanklist")
	@ResponseBody
	public AjaxResult getIssBanklist() {
		List<IssBank> list = issBankService.selectIssBankAll();
		return AjaxResult.successAndData("获取信用卡列表成功", list);
	}
	/**
	 * 获取用户银行卡列表(包括储蓄和信用卡)
	 *
	 * @param merchantId
	 *            用户ID
	 * @return
	 */
	@RequestMapping(value = "/bankList")
	@ResponseBody
	public AjaxResult bankList(String merchantId) {
		return merchantIncreaseLimitService.selectAllBank(merchantId);
	}
}
